// pages/control/control.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    ip: "IP地址", //IP地址
    port: 10000, //端口号
    message: "", //发送的数据
    
    strs: "", //接收到的数据
    light_1: false,
    light_2: true,
    height: null,
    width: null,
    color: "#000000",
    rgb: null,
    colorData: {
      //基础色相，即左侧色盘右上顶点的颜色，由右侧的色相条控制
      hueData: {
        colorStopRed: 255,
        colorStopGreen: 0,
        colorStopBlue: 0,
      },
      //选择点的信息（左侧色盘上的小圆点，即你选择的颜色）
      pickerData: {
        x: 0, //选择点x轴偏移量
        y: 480, //选择点y轴偏移量
        red: 0,
        green: 0,
        blue: 0,
        hex: '#000000'
      },
      //色相控制条的位置
      barY: 0
    },
    rpxRatio: 1 //此值为你的屏幕CSS像素宽度/750，单位rpx实际像素
  },


  // ChangeColor: function (e) {
  //   this.data.color = this.data.colorData.pickerData.hex
  //   console.log('color:' + this.data.color)
  //   console.log('RGB:' + this.data.colorData.pickerData.red + this.data.colorData.pickerData.green + this.data.colorData.pickerData.blue);
  //   this.setData({
  //     color: e.detail.value
  //   })
  // },


  /* 换图 */
  tapButtonLight_1(e) {
    if (this.data.light_1 == false && this.data.light_2 == true) {
      this.setData({
        light_1: true,
        light_2: false
      })
    } else {
      this.setData({
        light_1: false,
        light_2: true
      })
    }
  },

      //显示提示函数swish
      pushDataSuccess: function () {
        wx.showToast({
            title: this.data.strs,
            icon: 'sucess',
            image: '',
            duration: 800,
            mask: true,
        });
        setTimeout(function () {
            wx.hideToast();
        }, 500);
        // this.setData({
        //     swish: true
        // })
    },

    //按钮发送数据
    MsgOutput: function () {
      if (this.data.message == "") {
          this.setData({
              showOneButtonDialog: true
          })
      } else {
          this.setData({
              dialogShow: false,
              showOneButtonDialog: false
          })
          this.pushTheData(this.data.message)
          this.setData({
              'text': '',
          })
          this.setData({
              'message': ''
          })
          this.DataSuccess()
      }
  },

  

      /**转换字符 */
      uintToString: function uintToString(uintArray) {
        var encodedString = String.fromCharCode.apply(null, uintArray),
            decodedString = decodeURIComponent(escape(encodedString));
        return decodedString;
    },



      /*发送文本UDP字节函数*/

      pushTheData: function pushTheData(msg) {
        const udp = wx.createUDPSocket()
        // udp.close()
        udp.bind()
        udp.send({
            address: this.data.ip,
            port: this.data.port,
            length: 1024,
            message: this.data.rgb,
        })
        console.log("发送的数据为" + this.data.rgb);
        udp.onError()

        udp.offMessage()
        let that = this
        udp.onMessage(function (res) {
            // console.log(res)
            // console.log(res.message)
            var buf = res.message
            var decodedString = String.fromCharCode.apply(null, new Uint8Array(buf));
            let str = decodedString
            console.log('str == ' + str)
            // that.setData({
            //     strs: str,
            // })
            that.data.strs = str
            console.log("that.data.strs is " + that.data.strs)
            that.pushDataSuccess()
        })
        // udp.offMessage()
    },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: "加载中",
    });
    
        //初始化数据
        this.data.ip = getApp().globalData.ip
        this.data.port = getApp().globalData.port
        console.log(this.data.ip);
        console.log(this.data.port);
        
    /**获取页面大小 */
    this.setData({
      height: wx.getSystemInfoSync().windowHeight,
      width: wx.getSystemInfoSync().windowWidth,
    });
    console.log('height:' + this.data.height);
    console.log('width:' + this.data.width);

    setTimeout(function () {
      wx.hideLoading(); //必须调用hideLoading才能关闭loading
    }, 2000);
    var that = this;
    //设置rpxRatio
    wx.getSystemInfo({
      success(res) {
        
        that.setData({
          rpxRatio: res.screenWidth / 750
        })
      }
    })
  },
  //选择改色时触发（在左侧色盘触摸或者切换右侧色相条）
  
  onChangeColor(e) {
    this.data.color = this.data.colorData.pickerData.hex
    this.data.rgb = "rgb|" + this.data.colorData.pickerData.red + "|" + this.data.colorData.pickerData.green + "|" + this.data.colorData.pickerData.blue
    var that = this
    console.log('color:' + this.data.color)
    console.log('RGB:' + this.data.rgb)
      this.setData({
        color: this.data.color
      }),
    // that.setData({
    //   color: e.detail.color
    // })

    //返回的信息在e.detail.colorData中
    this.setData({
      colorData: e.detail.colorData
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    wx.showLoading({
      title: "加载中",
  });

  // 发送UDP字节函数
  const udp = wx.createUDPSocket();
  udp.bind(10000);
  udp.send({
      address: this.data.ip,
      port: this.data.port,
      length: 1024,
      message: 'I am Ready',
  });
  console.log("I am Ready");

  let that = this
  udp.onMessage(function (res) {
      console.log(res)
      console.log(res.message)
      var buf = res.message
      var decodedString = String.fromCharCode.apply(null, new Uint8Array(buf));
      let str = decodedString
      console.log('str===' + str)
      that.data.strs = str
      console.log(that.data.strs)
  })

  setTimeout(function () {
      wx.hideLoading(); //必须调用hideLoading才能关闭loading
  }, 2000);

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})